{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "from __future__ import absolute_import\n",
    "from __future__ import division\n",
    "from __future__ import print_function\n",
    "\n",
    "\n",
    "\n",
    "import tensorflow as tf\n",
    "from tensorflow.examples.tutorials.mnist import input_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.8.0\n",
      "=============导入数据\n",
      "Extracting D:/ministdata/input_data\\train-images-idx3-ubyte.gz\n",
      "Extracting D:/ministdata/input_data\\train-labels-idx1-ubyte.gz\n",
      "Extracting D:/ministdata/input_data\\t10k-images-idx3-ubyte.gz\n",
      "Extracting D:/ministdata/input_data\\t10k-labels-idx1-ubyte.gz\n",
      "=============构建模型\n",
      "=============增加一个隐藏层\n"
     ]
    }
   ],
   "source": [
    "FLAGS = None\n",
    "print (tf.__version__)\n",
    "print (\"=============导入数据\")\n",
    "#导入数据\n",
    "data_dir = 'D:/ministdata/input_data'\n",
    "mnist = input_data.read_data_sets(data_dir, one_hot=True)\n",
    "\n",
    "print (\"=============构建模型\")\n",
    "#x = tf.placeholder(tf.float32,[None, 784])\n",
    "#w = tf.Variable(tf.zeros([784, 10]))\n",
    "#b = tf.Variable(tf.zeros(10))\n",
    "print (\"=============增加一个隐藏层\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 一、增加一层隐藏层\n",
    "###  并将激活函数改为relu"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "h1_units=300#隐含层的输出节点数        300 3.8  0.9408    400 0.75 0.9323\n",
    "h2_units=10\n",
    "\n",
    "x = tf.placeholder(tf.float32,[None, 784])\n",
    "w = tf.Variable(tf.truncated_normal([784, h1_units],stddev=0.1))\n",
    "b = tf.Variable(tf.zeros(h1_units))\n",
    "\n",
    "\n",
    "\n",
    "h1x = tf.nn.relu(tf.matmul(x, w) + b)\n",
    "h1w = tf.Variable(tf.zeros([h1_units,h2_units]))\n",
    "h1b = tf.Variable(tf.zeros(h2_units))\n",
    "\n",
    "\n",
    "\n",
    "y = tf.matmul(h1x, h1w) + h1b\n",
    "            \n",
    "y_ = tf.placeholder(tf.float32,[None, 10])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 二、将学习率调整为0.9时，可以达到准确率为0.98以上\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.9803\n"
     ]
    }
   ],
   "source": [
    "#计算softmax是激活完了之后，再logist进行计算\n",
    "cross_entropy =tf.reduce_mean(#-tf.reduce_sum(y_*tf.log(y),reduction_indices=[1]))\n",
    "        tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))\n",
    "\n",
    "#训练\n",
    "train_step = tf.train.GradientDescentOptimizer(0.9).minimize(cross_entropy)\n",
    "sess = tf.Session()\n",
    "\n",
    "init_op = tf.global_variables_initializer()\n",
    "sess.run(init_op)\n",
    "\n",
    "\n",
    "\n",
    "for _ in range(3000):\n",
    "    batch_xs, batch_ys = mnist.train.next_batch(100)\n",
    "    sess.run(train_step, feed_dict={x:batch_xs, y_:batch_ys})\n",
    "\n",
    "correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))\n",
    "\n",
    "accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n",
    "\n",
    "print(sess.run(accuracy, feed_dict={x:mnist.test.images, y_:mnist.test.labels}))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
